package com.heima.wemedia.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.heima.model.admin.dto.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.entity.WmNews;
import com.heima.model.wemedia.entity.WmSensitive;

import com.heima.wemedia.dao.WmSensitiveDao;
import com.heima.wemedia.service.WmSensitiveService;
import com.heima.wemedia.util.WmThreadLocalUtil;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 自媒体敏感词表(WmSensitive)表服务实现类
 *
 * @author makejava
 * @since 2022-04-27 11:53:34
 */
@Service
public class WmSensitiveServiceImpl implements WmSensitiveService {
    @Resource
    private WmSensitiveDao wmSensitiveDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmSensitive queryById(Integer id) {
        return this.wmSensitiveDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param wmSensitive 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<WmSensitive> queryByPage(WmSensitive wmSensitive, PageRequest pageRequest) {
        long total = this.wmSensitiveDao.count(wmSensitive);
        return new PageImpl<>(this.wmSensitiveDao.queryAllByLimit(wmSensitive, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param wmSensitive 实例对象
     * @return 实例对象
     */
    @Override
    public WmSensitive insert(WmSensitive wmSensitive) {
        this.wmSensitiveDao.insert(wmSensitive);
        return wmSensitive;
    }

    /**
     * 修改数据
     *
     * @param wmSensitive 实例对象
     * @return 实例对象
     */
    @Override
    public WmSensitive update(WmSensitive wmSensitive) {
        this.wmSensitiveDao.update(wmSensitive);
        return this.queryById(wmSensitive.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.wmSensitiveDao.deleteById(id) > 0;
    }


    /**
     * 查询敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        Map<String, Object> queryParam = BeanUtil.beanToMap(dto);

        //queryParam.put("userId", WmThreadLocalUtil.get().getId());
        PageHelper.startPage(dto.getPage(), dto.getSize());

        //封装查询条件
        com.github.pagehelper.Page<WmSensitive> page = wmSensitiveDao.queryByMap(queryParam);

        //封装数据返回
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getResult());

        return result;

    }

    /**
     * 保存敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult save(SensitiveDto dto) {
        //校验参数
        if (dto.getSensitives() == null ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if ( WmThreadLocalUtil.get() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        WmSensitive wmSensitive =new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());
        wmSensitive.setCreatedTime(new Date());
        wmSensitive.setUpdatedTime(new Date());
        wmSensitiveDao.insert(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
